import sys
from baidu.aip import AipOcr
import random

appid_dict = [
    {"APP_ID": '17010327', 'API_KEY': 'X2MWCU1LG1PX5H6GAXgdlWD7', 'SECRET_KEY': 'vz6GZ6TkhSFvY3quqcuC3EG8oEW3kThB'}, \
    {"APP_ID": '14544448', 'API_KEY': 'yRZGUXAlCd0c9vQj1kAjBEfY', 'SECRET_KEY': 'sc0DKGy7wZ9MeWFGZnbscbRyoDB2IQlj'}, \
    {"APP_ID": '21372704', 'API_KEY': 'YKpXQwN5zj79g99fZK8i4Kn1', 'SECRET_KEY': 'RTIAaFrvvgHbej7eALMKmjR0uF93rHCQ'}, \
    {"APP_ID": '10040004', 'API_KEY': 'HPxjQ2nR0ClpNG9sXWBqGp47', 'SECRET_KEY': 'n7wsd5Le0hFExb5G2gs0LptPAdSeDUPR'}, \
    {"APP_ID": '16149264', 'API_KEY': 'yxYg9r4OuAs4fYvfcl8tqCYd', 'SECRET_KEY': 'yWg3KMds2muFsWs7MBSSFcgMQl8Wng4s'}, \
    {"APP_ID": '16611607', 'API_KEY': 'wAIXfXOUS8ztLa4FrK3rZex1', 'SECRET_KEY': '3b8nvjSGUZq0LPC18VVAizKYRBbny6Mq'}, \
    {"APP_ID": '15289864', 'API_KEY': 'j0pj5Y7HVElkLnmn2LEXKeyO', 'SECRET_KEY': 'FKVbH7EBcGy4DIaqPnXcqE47eACzn2W7'}, \
    {"APP_ID": '18096082', 'API_KEY': 'nTV3rarsqUNlzGibGw6Q3Bl5', 'SECRET_KEY': '4CbWiosVLTPzZbwBTZ86dE9bkTK70NQR'}, \
    {"APP_ID": '10040004', 'API_KEY': 'HPxjQ2nR0ClpNG9sXWBqGp47', 'SECRET_KEY': 'n7wsd5Le0hFExb5G2gs0LptPAdSeDUPR'}, \
    {"APP_ID": '17781881', 'API_KEY': '0L4q1HhmkcLq1QsNvd7rar5c', 'SECRET_KEY': 'yc1rd7PNt6YhoO8Zu7mWH9Z99avUxOXh'}, \
    {"APP_ID": '11206246', 'API_KEY': 'Xdp8WvFlZfG7eGQ54vlTUOc3', 'SECRET_KEY': '2AEFzhIp4TF8c3xzLpWOqqFE1p25K22f'}
    ]



# 读取图片
# filePath = "a.png"
def get_file_content(filePath):
    with open(filePath, 'rb') as fp:
        return fp.read()




# 定义参数变量
options = {
    'detect_direction': 'true',
    'language_type': 'CHN_ENG',
}


def baiduAip(filePath,keyword,x0=1,x1=1000):
    xindex = random.randint(1, 11) - 1
    print("appid index = " + str(xindex))
    APP_ID = appid_dict[xindex]['APP_ID']
    API_KEY = appid_dict[xindex]['API_KEY']
    SECRET_KEY = appid_dict[xindex]['SECRET_KEY']
    print(APP_ID)
    print(API_KEY)
    print(SECRET_KEY)

    # 初始化AipFace对象
    aipOcr = AipOcr(APP_ID, API_KEY, SECRET_KEY)

    print(keyword)

# 调用通用文字识别接口
    result = aipOcr.general(get_file_content(filePath), options)
# result = aipOcr.basicGeneral(get_file_content(filePath), options)
    print(result)
    words_result = result['words_result']

    print(words_result)
    if len(words_result)<1:
        return 0,0
    lenR = len(words_result)
    # for i in range(0,lenR-1):
    #     print(words_result[i]['words'])
    #     if keyword in words_result[i]['words']:
    #         y = words_result[i]['location']['top'] + words_result[i]['location']['height'] / 2
    #         x = words_result[i]['location']['left'] + words_result[i]['location']['width'] / 2
    #         print(int(x), int(y))
    #         return  (int(x), int(y))
    for item in words_result:
        print(item['words'])
        if keyword in item['words']:
            print(item["location"]['top'])
            print(item["location"]['left'])
            print(item["location"]['width'])
            print(item["location"]['height'])
            y = item["location"]['top'] + item["location"]['height']/ 2
            x = item["location"]['left'] + item["location"]['width'] / 2
            print(int(x), int(y))
            return (int(x), int(y))

    return 0,0

# baiduAip('a.jpeg',"我的",1,1000)
